package com.yanqu.road.server.cmd.activity;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.union.honors.eHonorsActivityType;
import com.yanqu.road.entity.winery.ActivityRank;
import com.yanqu.road.pb.cross.activity.CrossActivityProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.ActivityMgr;
import com.yanqu.road.server.manager.activity.ActivityRankMgr;
import com.yanqu.road.server.manager.union.honors.CrossUnionHonorsActivityMgr;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;

import java.util.List;

@Cmd(code = Protocol.C_CROSS_UNION_ACTIVITY_GET_REWARD, desc = "领取跨服商会活动商会奖励")
public class CrossUnionActivityGetRewardCmd extends CrossCommand {
    private int activityId;
    private int conditionId;
    private long userId;
    private String unionUid;
    private int position;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        CrossActivityProto.CrossActivityGetRewardReqMsg reqMsg = CrossActivityProto.CrossActivityGetRewardReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        conditionId = reqMsg.getConditionId();
        unionUid = reqMsg.getUnionUid();
        userId = packet.getPlayerId();
        position = reqMsg.getPosition();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        int myRank = -1;
        ActivityConditionInfo activityConditionInfo = ActivityMgr.getActivityConditionInfo(activityId, conditionId);
        if(null != activityConditionInfo){
            myRank = ActivityRankMgr.getCrossActivityUnionRank(activityId, unionUid);
        }
        CrossActivityProto.CrossActivityGetRewardRespMsg.Builder respMsg = CrossActivityProto.CrossActivityGetRewardRespMsg.newBuilder();
        respMsg.setActivityId(activityId);
        respMsg.setConditionId(conditionId);
        respMsg.setRank(myRank);
        respMsg.setPosition(position);
        respMsg.setUnionId(unionUid);
        YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.S_CROSS_ACTIVITY_UNION_GET_REWARD_NOTIFY, respMsg);
        MessageHelper.sendPacket(serverId, userId, pbMsg);

        //荣誉点奖励
        /*ActivityInfo activityInfo = ActivityMgr.getActivityInfo(activityId);
        if (activityInfo != null) {
            if(null == eHonorsActivityType.getHonorsType(activityInfo.getType(),activityInfo.getChildType())){
                log.info("getHonorsType error,type:{},childType:{}", activityInfo.getType(), activityInfo.getChildType());
                return;
            }
            int lowestRank = CrossUnionHonorsActivityMgr.getLowestRewardRank(activityInfo.getServerIdList().size(), eHonorsActivityType.getHonorsType(activityInfo.getType(), activityInfo.getChildType()));
            List<String> topUnions = ActivityRankMgr.getCrossActivityTopUnionsUidList(activityId, lowestRank);
            //CrossUserMgr.getLogger().debug("----  reward serverId {}", serverId);
            CrossUnionHonorsActivityMgr.add(activityId, topUnions, activityInfo.getType(), activityInfo.getChildType(), activityInfo.getServerIdList(),serverId);
        }*/
    }

    @Override
    public void sendData() {

    }
}
